function User(id, language, avatar, color)
{
	this.id = id;
	this.language = language;
	this.menu = new MainMenu();
	this.person = new Person();
	
	if(avatar=='')
	{
		this.avatar = '';
	}
	else if(avatar.length!=3 || isNaN(avatar))
	{
		this.avatar = '045.png'
	}
	else
	{
		this.avatar = avatar+'.png'
	}
	if(color=='')
	{
		this.color = '#b8cbe6'
	}
	else
	{
		this.color = '#'+color;
	}
}

User.prototype.getAvatar = function()
{
	return '<div class="avatar_box"><div class="avatar_background" style="background-color:'+this.color+'"><img src="'+(this.avatar!=''?'./extrat/avatar/64/'+this.avatar:'./css/user/img/noavatar.png')+'"></div></div>';
}

var current_user = null
var public_menu = null

var profil_popup = null
var picker_popup = null
var avatar_popup = null

var profil_color_picker = null
var profil_avatar_picker = null;
var profil_color = "000000";
var profil_avatar = "000.png";


function userProfil()
{
	if(current_user.anonymous) return;
	
	profil_popup = new Popup(360, 400);
	profil_popup.show(null);
	
	ajax.sendAsync("./action/user/user.php5?action=profil&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas)
	})
}

function userProfilCancel()
{
	profil_popup.hide()
	profil_popup = null
}

function userDisplayProfil()
{
	if(!current_user.anonymous)
	{
		profil_color = current_user.color.substring(1)
		profil_avatar = current_user.avatar
			
		userEvalLanguage('profil')
		if(document.getElementById("profil_tabs_connection"))
		{
			userEvalPassword('profil')
			
			document.getElementById("profil_saisie_password").onchange
			document.getElementById("profil_saisie_password").onkeyup = function(e)
			{
				userEvalPassword('profil');
				userCheckPassword('profil')
			}
		
			document.getElementById("profil_saisie_confirm").onchange
			document.getElementById("profil_saisie_confirm").onkeyup = function(e)
			{
				userCheckPassword('profil')
			}
		}
		document.getElementById("profil_saisie_country").onclick = function(event)
		{
			if(window.event)
			{
				target = window.event.srcElement
			}
			else
			{
				target = event.target
			}
			while(target.tagName != "LI" && target != null && target.getAttribute("fc_id")==null)
			{
				target = target.parentNode;
			}
			if(target==null || target.getAttribute("fc_id")==null)
			{
				return
			}
			document.getElementById("profil_saisie_language").value = target.getAttribute("fc_id")
			userEvalLanguage('profil')
		}
		
		var fct_tabs = function(tab)
		{
			var tabs = new Array('global', 'connection')
			for(var i=0; i<tabs.length; i++)
			{
				if(document.getElementById("profil_tabs_"+tabs[i]))
				{
					document.getElementById("profil_tabs_"+tabs[i]).removeAttribute("selected")
					document.getElementById("profil_body_"+tabs[i]).style.visibility = 'hidden'
				}
			}
			
			document.getElementById("profil_tabs_"+tab).setAttribute("selected", true)
			document.getElementById("profil_body_"+tab).style.visibility = 'visible'
			focusInput(document.getElementById("profil_body_"+tab));
		}
		
		document.getElementById("profil_tabs").onclick = function(event)
		{
			if(window.event)
			{
				target = window.event.srcElement
			}
			else
			{
				target = event.target
			}
			if(target.tagName != "LI" && target.getAttribute("tab")==null)
			{
				return
			}
			fct_tabs(target.getAttribute("tab"));
		}
		fct_tabs('global')
	}
}

function userProfilSave()
{
	var profil_form = document.getElementById("profil_form");
	disabledForm(profil_form, true)

	ajax.sendAsync("./action/user/user.php5?action=update&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas);
		disabledForm(profil_form, false)
		profil_popup.hide()
		profil_popup = null
	}, ajax.buildDatas(profil_form)+'&txt_color='+profil_color+'&txt_avatar='+profil_avatar.substring(0, 3))
}

function userProfilPicker()
{
	if(	picker_popup != null && picker_popup.showed)
	{
		return;
	}
	picker_popup = new Popup(390, 348);
	picker_popup.showSmall("cpicker", "«COLORPICKER_TITLE»", "javascript:userProfilPickerOk()", "", "userProfilPickerCancel()", false, 'cpicker', function()
	{
		profil_color_picker = new CPicker()
		profil_color_picker.color = profil_color
		profil_color_picker.init()
	})
}
function userProfilPickerCancel()
{
	picker_popup.hide()
	picker_popup = null;
}

function userProfilPickerOk()
{
	var color = profil_color_picker.getColor();
	document.getElementById("profil_saisie_color_sample").childNodes[0].childNodes[0].style.backgroundColor = "#"+color
	profil_color = color
	picker_popup.hide()
	picker_popup = null;
}

function userProfilAvatar()
{
	if(	avatar_popup != null && avatar_popup.showed)
	{
		return;		
	}
	avatar_popup = new Popup(390, 448);
	avatar_popup.showSmall("cavatar", "«AVATAR_TITLE»", "javascript:userProfilAvatarOk()", "", "userProfilAvatarCancel()", false, 'cavatar', function(datas)
	{
		ajax.sendAsync("./action/user/user.php5?action=list_avatar&u="+new Date().getTime(), function(datas)
		{
			ajax.eval(datas)
			profil_avatar_picker = profil_avatar;
			if(document.getElementById("avatar_"+profil_avatar_picker)!=null)
			{
				document.getElementById("avatar_"+profil_avatar_picker).setAttribute("selected", true)
				focusElement(document.getElementById("avatar_"+profil_avatar_picker))
			}
			document.getElementById("user_profil_avatar_content").onclick = function(event)
			{
				if(window.event)
				{
					target = window.event.srcElement
				}
				else
				{
					target = event.target
				}
				if(target.tagName != "IMG")
				{
					return;
				}
				if(document.getElementById("avatar_"+profil_avatar_picker)!=null)
				{
					document.getElementById("avatar_"+profil_avatar_picker).removeAttribute("selected")
				}
				profil_avatar_picker = target.id.substring(7)
				document.getElementById("avatar_"+profil_avatar_picker).setAttribute("selected", true)
			}
			document.getElementById("user_profil_avatar_content").ondblclick = function(event)
			{
				if(window.event)
				{
					target = window.event.srcElement
				}
				else
				{
					target = event.target
				}
				if(target.tagName != "IMG")
				{
					return;
				}
				if(document.getElementById("avatar_"+profil_avatar_picker)!=null)
				{
					document.getElementById("avatar_"+profil_avatar_picker).removeAttribute("selected")
				}
				profil_avatar_picker = target.id.substring(7)
				userProfilAvatarOk()
			}
		})
	})
}
function userProfilAvatarCancel()
{
	avatar_popup.hide()
	avatar_popup = null;
}
function userProfilAvatarOk()
{
	var avatar = profil_avatar_picker;
	document.getElementById("profil_saisie_color_sample").childNodes[0].childNodes[0].childNodes[0].src=(avatar!=''?'./extrat/avatar/64/'+avatar:'./css/user/img/noavatar.png')
	profil_avatar = avatar
	avatar_popup.hide()
	avatar_popup = null;
}

function userPageDisplay(language)
{
	var name = current_user.person.firstName+" "+current_user.person.name
	if(name==" ")
	{
		name = "&nbsp;"
	}
	document.getElementById("home_user_box_title").innerHTML = name
	document.getElementById("home_avatar_box").innerHTML = current_user.getAvatar();

	current_user.menu.display();

	document.getElementById("home_user_box_menu_profil").style.display=(current_user.anonymous?"none":"block")
	document.getElementById("home_user_box_menu_logout").style.display=(current_user.anonymous?"none":"block")
	document.getElementById("home_user_box_menu_login").style.display=(current_user.anonymous?"block":"none")
	document.getElementById("home_user_box_menu_register").style.display=(current_user.anonymous?"block":"none")

	if(language!=null)
	{
		document.getElementById("home_user_box_menu_profil").getElementsByTagName("A")[0].innerHTML = language[0]
		document.getElementById("home_user_box_menu_logout").getElementsByTagName("A")[0].innerHTML = language[1]
		document.getElementById("home_user_box_menu_login").getElementsByTagName("A")[0].innerHTML = language[2]
		document.getElementById("home_user_box_menu_register").getElementsByTagName("A")[0].innerHTML = language[3]
	}
}

function userConnectionRefreshLogin()
{
	ajax.sendAsync("./action/user/user.php5?action=refeshlogin&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas)
	})
}

function userCheckPassword(page)
{
	if(document.getElementById(page+'_saisie_password').value=='' || document.getElementById(page+'_saisie_confirm').value=='')
	{
		document.getElementById(page+'_saisie_confirm').removeAttribute('validity')
	}
	else if(document.getElementById(page+'_saisie_password').value==document.getElementById(page+'_saisie_confirm').value)
	{
		document.getElementById(page+'_saisie_confirm').setAttribute('validity', 1)
	}
	else
	{
		document.getElementById(page+'_saisie_confirm').setAttribute('validity', 0)
	}
}

var userPasswordMessages = new Array('Faible', 'Moyen', 'élevé', 'Optimal')
function userEvalPassword(page)
{
	var password = document.getElementById(page+'_saisie_password').value
	var pourcent = evalPassword(password);
	
	var color_start = [255, 0, 0]
	var color_middle = [255, 145, 0]
	var color_stop = [144, 255, 0]
	
	if(pourcent<0)
	{
		document.getElementById(page+'_saisie_password_level_message').innerHTML = ''
		document.getElementById(page+'_saisie_password_level_progress').style.width='0px'
	}
	else
	{
		if(pourcent==100)
			var level = 3
		else
			var level = Math.min(2, Math.floor(3*pourcent/100))

		document.getElementById(page+'_saisie_password_level_message').innerHTML = userPasswordMessages[level]
		document.getElementById(page+'_saisie_password_level_progress').style.width=(1.42*pourcent)+'px'
		
		var color
		if(pourcent<50)
		{
			color = [Math.round(color_start[0]+(color_middle[0]-color_start[0])*pourcent/50)
			, Math.round(color_start[1]+(color_middle[1]-color_start[1])*pourcent/50)
			, Math.round(color_start[2]+(color_middle[2]-color_start[2])*pourcent/50)]
		}
		else
		{
			color = [Math.round(color_middle[0]+(color_stop[0]-color_middle[0])*(pourcent-50)/50)
			, Math.round(color_middle[1]+(color_stop[1]-color_middle[1])*(pourcent-50)/50)
			, Math.round(color_middle[2]+(color_stop[2]-color_middle[2])*(pourcent-50)/50)]
		}
		document.getElementById(page+'_saisie_password_level_progress').style.backgroundColor='rgb('+color[0]+', '+color[1]+', '+color[2]+')'
	}
}
function evalPassword(passwd)
{
	var intScore   = 0

	if (passwd.length==0 || !passwd.length)			// length 0
		intScore = -1
	else if (passwd.length>0 && passwd.length<5)	// length between 1 and 4
		intScore = (intScore+3)
	else if (passwd.length>4 && passwd.length<8)	// length between 5 and 7
		intScore = (intScore+6)
	else if (passwd.length>7 && passwd.length<12)	// length between 8 and 15
		intScore = (intScore+12)
	else if (passwd.length>11)						// length 16 or more
		intScore = (intScore+18)

	if (passwd.match(/[a-z]/))						// [verified] at least one lower case letter
		intScore = (intScore+1)
	if (passwd.match(/[A-Z]/))						// [verified] at least one upper case letter
		intScore = (intScore+5)
	if (passwd.match(/\d+/))						// [verified] at least one number
		intScore = (intScore+5)
	if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))	// [verified] at least three numbers
		intScore = (intScore+5)

	if (passwd.match(/.[^a-zA-Z0-9]/))				// [verified] at least one special character
		intScore = (intScore+5)
	if (passwd.match(/(.*[^a-zA-Z0-9].*[^a-zA-Z0-9])/))
		intScore = (intScore+5)

	if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))	// [verified] both upper and lower case
		intScore = (intScore+2)
	if (passwd.match(/(\d.*\D)|(\D.*\d)/))				// [FAILED] both letters and numbers, almost works because an additional character is required
		intScore = (intScore+2)
	if (passwd.match(/([a-zA-Z0-9].*[^a-zA-Z0-9])|([^a-zA-Z0-9].*[a-zA-Z0-9])/))
		intScore = (intScore+2)

	var level_max = 50;

	if(intScore<0) return -1
	return (100*(intScore/(level_max)))
}

function userEvalLanguage(page)
{
	var language = document.getElementById(page+'_saisie_language').value
	document.getElementById(page+'_saisie_country_fr').removeAttribute('selected')
	document.getElementById(page+'_saisie_country_gb').removeAttribute('selected')
	if(document.getElementById(page+'_saisie_country_'+language))
	{
		document.getElementById(page+'_saisie_country_'+language).setAttribute('selected', true)
	}
	else
	{
		document.getElementById(page+'_saisie_language').value = 'fr'
		document.getElementById(page+'_saisie_country_fr').setAttribute('selected', true)
	}
}
